/**
 * @author 黄波
 * 创建日期：2011Sep 19, 2011
 * TODO// N/A
 */
package com.platform.res.bo;

import java.util.List;

import com.platform.bean.PageBean;
import com.platform.dao.BaseDao;
import com.platform.pojo.DownloadRecord;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResReviews;
import com.platform.pojo.ResType;
import com.platform.pojo.TmpRes;
import com.platform.pojo.UserInfo;

/**
 * 资源业务逻辑
 */
public class ResBo {

	private static ResBo instance;
	private ResBo() {
	}
	public static ResBo getInstance() {
		instance = new ResBo();
		return instance;
	}

	/**
	 * 查询所有的资源类型
	 * @return
	 */
	public List<ResType> getTypes() {
		String hql = "from ResType";
		return BaseDao.getList(hql);
	}
	/**
	 * 根据父ID返回资源类型
	 * @param superId
	 * @return
	 */
	public List<ResType> getTypes(String superId) {
		String hql = "from ResType where superId=" + superId;
		return BaseDao.getList(hql);
	}
	
	/**
	 * 添加资源类型
	 * @param type
	 * @return
	 */
	public boolean addType(ResType type) {
		return BaseDao.add(type);
	}
	
	/**
	 * 删除资源类型
	 * @param id
	 * @return
	 */
	public boolean deleteType(String id) {
		String sql = "DELETE FROM res_type WHERE id=" + id;
		return BaseDao.executeSql(sql);
	}
	
	/**
	 * 获得资源类型
	 * @param id
	 * @return
	 */
	public ResType getType(String id) {
		String hql = "from ResType where id=" + id;
		return (ResType)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 更新类型
	 * @param obj
	 * @return
	 */
	public boolean updateType(ResType obj) {
		return BaseDao.update(obj);
	}
	
	/**
	 * 分页获得资源信息
	 * @param pageBean
	 * @return
	 */
	public Object[] getInfos(PageBean pageBean) {
		String hql = "from ResInfo order by isRed desc, uploadTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	/**
	 * 分页获得临时资源信息
	 * @param pageBean
	 * @return
	 */
	public Object[] getTmpRes(PageBean pageBean, String userId) {
		String hql = "from TmpRes where uploadUserId=" + userId + " order by uploadTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	/**
	 * 添加资源信息
	 * @param obj
	 * @return
	 */
	public boolean addInfo(ResInfo obj) {
		return BaseDao.add(obj);
	}
	
	/**
	 * 删除资源信息
	 * @param id
	 * @return
	 */
	public boolean deleteInfo(String id) {
		String sql = "DELETE FROM res_info WHERE id=" + id;
		return BaseDao.executeSql(sql);
	}
	
	/**
	 * 获得资源信息
	 * @param id
	 * @return
	 */
	public ResInfo getInfo(String id) {
		String hql = "from ResInfo where id=" + id;
		return (ResInfo)BaseDao.getUniqueResult(hql);
	}
	
	/**
	 * 更新资源信息
	 * @param obj
	 * @return
	 */
	public boolean updateInfo(ResInfo obj) {
		return BaseDao.update(obj);
	}
	
	/**
	 * 查询推荐资源列表
	 * @return
	 */
	public List<ResInfo> getRecmdResInfo() {
		return BaseDao.getList("from ResInfo where shareWay=0 and isRed=1 order by uploadTime desc");
	}
	
	/**
	 * 获得下载排行 （十个资源）
	 * @return
	 */
	public Object[] getFaResInfo() {
		return BaseDao.getSpiltList(1, 10, "from ResInfo where shareWay=0 order by downloadCount desc");
	}
	
	/**
	 * 根据ID，查询最近的十条资源
	 * @param typeId
	 * @return
	 */
	public Object[] getResInfoByType(String typeId) {
		return BaseDao.getSpiltList(1, 10, "from ResInfo t1 where shareWay=0 and t1.isRed=0 and t1.typeId in (select id from ResType where superId="+ typeId +") order by t1.uploadTime desc");
	}
	
	/**
	 * 根据类别id，分页查询
	 * @param type
	 * @param pageBean
	 * @return
	 */
	public Object[] getInfosByType(String typeId, PageBean pageBean) {
		String hql = "from ResInfo where shareWay=0 and typeId in (select id from ResType where id=" + typeId + " or superId="+ typeId +") order by isRed desc, uploadTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	/**
	 * 按条件搜索资源
	 * @param type 0：资源名称 1：共享人
	 * @param query
	 * @param pageBean
	 * @return
	 */
	public Object[] searchRes(String type, String query, PageBean pageBean) {
		if(type != null) {
			if(type.equals("0")) {
				//按资源名称模糊搜索
				String hql = "from ResInfo where shareWay=0 and name like '%" + query + "%' order by isRed, uploadTime desc";
				if(pageBean == null) {
					//如传入参数为空，则使用默认的分页
					pageBean = new PageBean();
					return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
				} else {
					//使用传入的page bean 进行分页查询
					return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
				}
			} else if(type.equals("1")) {
				//按共享人模糊搜索
				String hql = "from ResInfo where shareWay=0 and uploadUserId in (select id from UserInfo where realName like '%" + query + "%') order by isRed, uploadTime desc";
				if(pageBean == null) {
					//如传入参数为空，则使用默认的分页
					pageBean = new PageBean();
					return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
				} else {
					//使用传入的page bean 进行分页查询
					return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
				}
			}
		}
		
		return null;
		
	}
	
	
	/**
	 * 添加下载记录
	 * @param obj
	 * @return
	 */
	public boolean addDownloadRecord(DownloadRecord obj) {
		return BaseDao.add(obj);
	}
	
	/**
	 * 根据用户ID和资源ID，取下载记录
	 * @param userId
	 * @param resId
	 * @return
	 */
	public DownloadRecord getDownloadRecord(int userId, int resId) {
		return (DownloadRecord) BaseDao.getUniqueResult("from DownloadRecord where userId=" + userId + " and resId=" + resId);
	}
	
	/**根据课程ID,查询该课程下的资源信息列表
	 * @param courseId
	 * @return
	 */
	public List<ResInfo> getResInfoByFolderId(String folderId) {
		
		return BaseDao.getList("from ResInfo where folderId=" + folderId);
	}
	
	/**
	 * 添加资源评论
	 * @param obj
	 * @return
	 */
	public boolean addResReview(ResReviews obj) {
		return BaseDao.add(obj);
	}
	
	/**
	 * 分页查询资源评论
	 * @param pageBean
	 * @return
	 */
	public Object[] getResReviewsList(PageBean pageBean) {
		String hql = "from ResReviews order by commentTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	
	/**
	 * 根据资源ID，分页查询资源评论
	 * @param pageBean
	 * @return
	 */
	public Object[] getResReviewsList(String resId, PageBean pageBean) {
		String hql = "from ResReviews where resId=" + resId + " order by commentTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}

	/**
	 * 删除资源评论
	 * @param id
	 * @return
	 */
	public boolean deleteResReview(String id) {
		return BaseDao.executeSql("DELETE FROM res_reviews WHERE id=" + id);
	}
	
	/**
	 * 根据用户ID及资源ID，查询资源评论
	 * @param resId
	 * @param userId
	 * @return
	 */
	public ResReviews getResReview(String userId, String resId) {
		return (ResReviews)BaseDao.getUniqueResult("from ResReviews where userId=" + userId + " and resId=" + resId);
	}
	
	/**
	 * 根据资源ID，查询资源被评论次数
	 * @param resId
	 * @return
	 */
	public Long getReviewCount(String resId) {
		return (Long)BaseDao.getUniqueResult("select count(*) from ResReviews where resId=" + resId);
	}
	
	/**
	 * 获得资源总数
	 * @return
	 */
	public Long getResInfoCount() {
		return (Long)BaseDao.getUniqueResult("select count(*) from ResInfo");
	}
	
	/**
	 * 查询下载量最多的资源
	 * @return
	 */
	public ResInfo getFaRes() {
		Object[] objs = BaseDao.getSpiltList(1, 1, "from ResInfo order by downloadCount desc");
		List list = (List) objs[1];
		if(list != null && list.size() > 0) {
			return (ResInfo)list.get(0);
		}
		return null;
	}
	/**
	 * 根据用户ID,分布查询该用户已上传的资源
	 * @param userId
	 * @param pageBean 
	 * @return
	 */
	public Object[] getMyUploadRes(Integer userId, PageBean pageBean) {

		String hql = "from ResInfo where uploadUserId=" + userId + " order by uploadTime desc";
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			pageBean.setPageSize(8);
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			pageBean.setPageSize(8);
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	
	/**
	 * 查询我已下载的资源
	 * @param userId
	 * @param pageBean
	 */
	public Object[] getMyDownloadRes(Integer userId, PageBean pageBean) {

		String hql = "from ResInfo t1, DownloadRecord t2 where t1.id=t2.resId and t2.userId=" + userId;
		if(pageBean == null) {
			//如传入参数为空，则使用默认的分页
			pageBean = new PageBean();
			pageBean.setPageSize(8);
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		} else {
			pageBean.setPageSize(8);
			//使用传入的page bean 进行分页查询
			return BaseDao.getSpiltList(pageBean.getCurrPage(), pageBean.getPageSize(), hql);
		}
	}
	
	/**
	 * 根据资源ID，查询被下载次数
	 * @param resId
	 * @return
	 */
	public Long getDownloadCount(Integer resId) {
		return (Long) BaseDao.getUniqueResult("select count(*) from DownloadRecord where resId=" + resId);
	}
	
	/**
	 * @param 添加暂存文件
	 */
	public boolean addTmpRes(TmpRes tmpRes) {
		return BaseDao.add(tmpRes);
	}
	
	/**
	 * 删除服务器暂存文件
	 * @param id
	 * @return
	 */
	public boolean deleteTmpRes(String id) {
		return BaseDao.executeSql("DELETE FROM tmp_res where id=" + id);
	}
	/**
	 * 查询临时资源实体
	 * @param id
	 * @return
	 */
	public TmpRes getTmpRes(String id) {
		
		return (TmpRes) BaseDao.getUniqueResult("from TmpRes where id=" + id);
	}
}
